package com.hwhp.web.console.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.hwhp.common.Base64;
import com.hwhp.common.BaseAction;
import com.hwhp.common.Pager;
import com.hwhp.entity.Deptment;
import com.hwhp.entity.YzUser;
import com.hwhp.service.console.DepartService;
import com.hwhp.service.console.SPositionService;
import com.hwhp.service.console.UserService;
import com.hwhp.util.DateUtils;


/**
 * @author lyf Jul 7, 2009 desc:
 */
public class AdminUser extends BaseAction {
	
	private UserService userService ;
	
	private DepartService departService ;
	
	private SPositionService positionService ;
	/**
	 * 
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userIndex() {
		return "usermanager.init.success";
	}

	/**
	 * 
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userTree(){
		HttpServletRequest request = this.getHttpRequest();
		String deptid = request.getParameter("deptid");
		List<Deptment> list = departService.getChildDepts(deptid);
		request.setAttribute("deptList", list);
		//return mapping.findForward("usermanager.tree.success");
		return "usermanager.tree.success";
	}

	/**
	 * 人员列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userList() {
		HttpServletRequest request = this.getHttpRequest();
		String deptid = request.getParameter("deptid");
		
		if (deptid == null || "".equals(deptid) || "null".equals(deptid)) {
			deptid = "-1";
		}
		String pageNumber = request.getParameter("pageNumber");
		String title = request.getParameter("title");
		if(title == null){
			title = "";
		}
		int pgNo = 1;
		try{
			pgNo = Integer.parseInt(pageNumber);
		}catch(Exception e){
			pgNo = 1;
			e.printStackTrace();
		}
		Pager page = new Pager();
		page = userService.getUserPagerByDeptId(title, deptid, pgNo);
		
		request.setAttribute("page",page);
		request.setAttribute("deptid", deptid);
		request.setAttribute("title", title);
		
		return "usermanager.list.success";
	}

	/**
	 * 人员编辑
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userEdit() {
		HttpServletRequest request = this.getHttpRequest();
		String userid = request.getParameter("userid");
		String deptid = request.getParameter("deptid");
		String act = request.getParameter("act");
		List spList = new ArrayList();
		spList = positionService.getAllPs();
		request.setAttribute("spList", spList);
		if (act != null && "add".equals(act)) {
			YzUser userObj = new YzUser();
			Deptment deptObj = departService.getDepart(deptid);
			request.setAttribute("deptObj", deptObj);
			request.setAttribute("userObj", userObj);
			request.setAttribute("act", "add");
		}
		
		if (act != null && "edit".equals(act)) {
			Deptment deptObj = departService.getDepart(deptid);
			YzUser userObj = userService.getUser(userid);
			request.setAttribute("deptObj", deptObj);
			request.setAttribute("userObj", userObj);
			request.setAttribute("act", "edit");
		}
		//return mapping.findForward("usermanager.edit.success");
		return "usermanager.edit.success";
	}
    /**
     * 新增人员
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
	public String insertUser() {
		HttpServletRequest request = this.getHttpRequest();
		boolean success = false;
		String act = request.getParameter("act");
		String userId = request.getParameter("userid");
		String userName = request.getParameter("username");
		String deptId = request.getParameter("deptid");
		String positionId = request.getParameter("positionId");
		String birthFrom = request.getParameter("birthFrom");
		String birthDate = request.getParameter("birthdate");
		String createDate = request.getParameter("createdate");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		
		String wenhua = request.getParameter("wenhua");
		//String birthFrom = request.getParameter("birthFrom");
		String minzu = request.getParameter("minzu");
		String beiZhu = request.getParameter("beiZhu");
		Date bDate = DateUtils.format(birthDate);
		Date lDate = DateUtils.format(createDate);
		YzUser u = new YzUser();
		List spList = new ArrayList();
		spList = positionService.getAllPs();
		
		
		if("add".equals(act)){
			u.setBeizhu(beiZhu);
			u.setBirthDate(bDate);
			u.setBirthFrom(birthFrom);
			u.setDeptId(deptId);
			u.setMinzhu(minzu);
			u.setUserName(userName);
			u.setWenhua(wenhua);
			u.setCreateDate(lDate);
			u.setDeptId(deptId);
			u.setEmail(email);
			u.setMobile(mobile);
			u.setPositionId(positionId);
			u.setPassword(Base64.encode("111111".getBytes()));
			success = userService.insertUser(u);
		}
		if("edit".equals(act)){
			u = userService.getUser(userId);
			u.setBeizhu(beiZhu);
			u.setBirthDate(bDate);
			u.setBirthFrom(birthFrom);
			u.setDeptId(deptId);
			u.setMinzhu(minzu);
			u.setUserName(userName);
			u.setWenhua(wenhua);
			u.setCreateDate(lDate);
			u.setDeptId(deptId);
			u.setEmail(email);
			u.setMobile(mobile);
			u.setPositionId(positionId);
			success = userService.editUser(u);
			
			request.setAttribute("userObj", u);
		}
		Deptment deptObj = departService.getDepart(deptId);
		request.setAttribute("deptObj", deptObj);
		request.setAttribute("act",act);
		request.setAttribute("spList", spList);
		return "usermanager.edit.success" ;
		//return mapping.findForward("usermanager.edit.success");
	}

	public String editUser() {
		boolean success = false;
		HttpServletRequest request = this.getHttpRequest();
		String userName = request.getParameter("userName");
		String userId = request.getParameter("userId");
		String deptId = request.getParameter("deptId");
		String courseId = request.getParameter("courseId");
		String birthDate = request.getParameter("birthDate");
		String luDate = request.getParameter("luDate");
		String junxian = request.getParameter("junxian");
		String zhiwu = request.getParameter("zhiwu");
		String wenhua = request.getParameter("wenhua");
		String birthFrom = request.getParameter("birthFrom");
		String minzhu = request.getParameter("minzhu");
		String beiZhu = request.getParameter("beiZhu");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		String positionId = request.getParameter("positionId");
		Date bDate = DateUtils.format(birthDate);
		Date lDate = DateUtils.format(luDate);
		YzUser u = new YzUser();
//		u.setUserId(userId);
//		u.setBeizhu(beiZhu);
//		u.setBirthDate(bDate);
//		u.setBirthFrom(birthFrom);
//		u.setCourseId(courseId);
//		u.setDeptId(deptId);
//		u.setJunxian(junxian);
//		u.setLuDate(lDate);
//		u.setMinzhu(minzhu);
//		u.setUserName(userName);
//		u.setWenhua(wenhua);
//		u.setZhiwu(zhiwu);
		u.setBeizhu(beiZhu);
		u.setBirthDate(bDate);
		u.setBirthFrom(birthFrom);
		u.setDeptId(deptId);
		u.setMinzhu(minzhu);
		u.setUserName(userName);
		u.setWenhua(wenhua);
		u.setCreateDate(lDate);
		u.setDeptId(deptId);
		u.setEmail(email);
		u.setMobile(mobile);
		u.setPositionId(positionId);
		
		success = userService.editUser(u);
		return this.userEdit();
	}
    /**
     * 人员删除
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
	public String deleteUser() {
		HttpServletRequest request = this.getHttpRequest();
		String userId = request.getParameter("userid");
		//String deptId = request.getParameter("deptid");
		boolean success = false;
		YzUser u = userService.getUser(userId);
		success = userService.deleteUser(u);
		return this.userList();
		//return this.userList(mapping, form, request, response);
	}

	
	public void setDepartService(DepartService departService) {
		this.departService = departService;
	}

	public DepartService getDepartService() {
		return departService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setPositionService(SPositionService positionService) {
		this.positionService = positionService;
	}

	public SPositionService getPositionService() {
		return positionService;
	}
}
